package com.tf8.treasure.controller;

import com.tf8.treasure.domain.ResponseInfo;
import com.tf8.treasure.entity.LtUser;
import com.tf8.treasure.entity.Session;
import com.tf8.treasure.enums.ReturnType;
import com.tf8.treasure.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
    @Autowired
    private SessionService sessionService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    protected Session getSession(){
        String cookie = httpServletRequest.getHeader("cookie");
        if(StringUtils.isEmpty(cookie)){
            return null;
        }
        return sessionService.readSession(cookie);
    }

    protected Session addUserToSession(String cookie,LtUser ltUser){
        if(StringUtils.isEmpty(cookie)){
            return null;
        }
        Session session = new Session();
        session.setLtUser(ltUser);
        session.setCookie(cookie);
        sessionService.create(cookie,session);
        return session;
    }

    protected ResponseEntity returnSuccess(Object o){
        return ResponseEntity.ok(new ResponseInfo<>(ReturnType.SUCCESS, o));
    }

}
